using System;
using FireStarter.Base;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.UI;
using FireStarterModeller.UI.Relationship;
using Habanero.Faces.Base;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI.Relationships
{
    [TestFixture]
    public class TestCreateRelationshipWizardStep3
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            UITestUtils.SetupTest();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            UITestUtils.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        [Test]
        public void Test_CreateStep_WithNullRelationship_ThrowsError()
        {
            //---------------Set up test pack-------------------
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                new CreateRelationshipWizardStep3(null, GlobalUIRegistry.ControlFactory);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("relationship", ex.ParamName);
            }
        }

        [Test]
        public void Test_CreateStep_WithNullControlFactory_ThrowsError()
        {
            //---------------Set up test pack-------------------
            DMClass ownerClass = TestUtilsDMClass.CreateSavedDMClass();
            DMRelationshipCreator relationshipCreator = new DMRelationshipCreator(ownerClass);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            try
            {
                new CreateRelationshipWizardStep3(relationshipCreator, null);
                Assert.Fail("expected ArgumentNullException");
            }
                //---------------Test Result -----------------------
            catch (ArgumentNullException ex)
            {
                StringAssert.Contains("Value cannot be null", ex.Message);
                StringAssert.Contains("controlFactory", ex.ParamName);
            }
        }

        [Test]
        public void Test_Construct_ShouldSetUpControlsOnStep()
        {
            DMClass ownerClass = TestUtilsDMClass.CreateSavedDMClass();
            DMRelationshipCreator relationshipCreator = new DMRelationshipCreator(ownerClass);
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            CreateRelationshipWizardStep3 step3 = new CreateRelationshipWizardStep3(relationshipCreator,
                                                                                    GlobalUIRegistry.ControlFactory);
            //---------------Test Result -----------------------
            Assert.AreEqual(1, step3.Controls.Count);
            DMRelationshipEditorControl control = (DMRelationshipEditorControl) step3.Controls[0];
            Assert.IsInstanceOf(typeof(DMRelationshipEditorControl),control);
//            Assert.AreEqual(relationshipCreator.Relationship, control.BusinessObject);
        }

//
//        [Test]
//        public void Test_CreateStep_WithRelationship_SetRelationship()
//        {
        //---------------Set up test pack-------------------
//            DMClass ownerClass = TestUtilsDMClass.CreateSavedDMClass();
//            DMRelationshipCreator relationshipCreator = new DMRelationshipCreator(ownerClass);
        //---------------Assert Precondition----------------
        //---------------Execute Test ----------------------
//
//
//            CreateRelationshipWizardStep3 step3 = new CreateRelationshipWizardStep3(relationshipCreator, GlobalUIRegistry.ControlFactory);
        //---------------Test Result -----------------------
//            Assert.IsNotNull(step3);
//            Assert.AreSame(relationshipCreator.Relationship, step3.Relationship);
//            Assert.AreEqual("step3: Select Create Foreign Key", step3.HeaderText);
//        }

//
//        [Test]
//        public void Test_InitialiseStep_ShouldDefaultControls()
//        {
        //---------------Set up test pack-------------------]
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3) Getstep3();
        //---------------Execute Test ----------------------
//            step3.InitialiseStep();
        //---------------Test Result -----------------------
//            Assert.IsNotNull(step3.CardinalityComboBox.SelectedItem);
//            Assert.AreEqual("One-To-Many",step3.CardinalityComboBox.SelectedItem.ToString());
//            string message;
//            Assert.IsTrue(step3.CanMoveOn(out message), "Defaults All Relationship Props to Sensible Values " + message);
//        }
//
//        [Test]
//        public void Test_InitialiseStep_ShouldBeAbleToGetCreatedRelationship()
//        {
        //---------------Set up test pack-------------------]
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3) Getstep3();
        //---------------Execute Test ----------------------
//            step3.InitialiseStep();
        //---------------Test Result -----------------------
//            Assert.AreSame(step3.Relationship, step3.CreatedRelationship);
//        }
//
//        [Test]
//        public void Test_CanMoveBack_True()
//        {
        //---------------Set up test pack-------------------
//            IWizardStep step3 = Getstep3();
        //---------------Assert Precondition----------------
//
        //---------------Execute Test ----------------------
//            bool canMoveBack = step3.CanMoveBack();
        //---------------Test Result -----------------------
//            Assert.IsTrue(canMoveBack);
//        }
//
//        [Test]
//        public void Test_Initialise_ShouldSetUpCreateForeignKeyTrue()
//        {
        //---------------Set up test pack-------------------
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3)Getstep3();
        //---------------Assert Precondition----------------
//            Assert.IsFalse(step3.IsInitialised);
        //---------------Execute Test ----------------------
//            step3.InitialiseStep();
//            bool createFK = step3.MustCreateForeignKey;
        //---------------Test Result -----------------------
//            Assert.IsTrue(createFK);
//            Assert.IsTrue(step3.CreateFKCheckBox.Checked);
//        }
//
//        [Test]
//        public void Test_CreateFKCheckBox_False_ShouldSetCreateForeignKeyFalse()
//        {
        //---------------Set up test pack-------------------
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3)Getstep3();
//            step3.InitialiseStep();
        //---------------Assert Precondition----------------
//            Assert.IsTrue(step3.IsInitialised);
        //---------------Execute Test ----------------------
//            step3.CreateFKCheckBox.Checked = false;
//            bool createFK = step3.MustCreateForeignKey;
        //---------------Test Result -----------------------
//            Assert.IsFalse(createFK);
//            Assert.IsFalse(step3.CreateFKCheckBox.Checked);
//        }
//
//        [Test]
//        public void Test_CreateFKCheckBox_False_ShouldDisableCardinalityComboBox()
//        {
        //---------------Set up test pack-------------------
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3)Getstep3();
//            step3.InitialiseStep();
        //---------------Assert Precondition----------------
//            Assert.IsTrue(step3.IsInitialised);
        //---------------Execute Test ----------------------
//            step3.CreateFKCheckBox.Checked = false;
//            bool createFK = step3.MustCreateForeignKey;
        //---------------Test Result -----------------------
//            Assert.IsFalse(createFK);
//            Assert.IsFalse(step3.CreateFKCheckBox.Checked);
//            Assert.IsFalse(step3.CardinalityComboBox.Enabled);
//        }
//
//        [Test]
//        public void Test_CreateFKCheckBox_True_ShouldEnableCardinalityComboBox()
//        {
        //---------------Set up test pack-------------------
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3)Getstep3();
//            step3.InitialiseStep();
//            step3.CreateFKCheckBox.Checked = false;
        //---------------Assert Precondition----------------
//            Assert.IsTrue(step3.IsInitialised);
//            Assert.IsFalse(step3.MustCreateForeignKey);
//            Assert.IsFalse(step3.CreateFKCheckBox.Checked);
        //---------------Execute Test ----------------------
//            step3.CreateFKCheckBox.Checked = true;
//            bool createFK = step3.MustCreateForeignKey;
        //---------------Test Result -----------------------
//            Assert.IsTrue(createFK);
//            Assert.IsTrue(step3.CreateFKCheckBox.Checked);
//            Assert.IsTrue(step3.CardinalityComboBox.Enabled);
//        }
//
//        [Test]
//        public void Test_CanMoveOn_WhenSelectedCreateFK_AndCardinalitySet_ShouldBeTrue()
//        {
        //---------------Set up test pack-------------------
//            DMRelationshipCreator relationshipCreator;
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3)Getstep3(out relationshipCreator);
//            step3.InitialiseStep();
//            step3.CreateFKCheckBox.Checked = true;
//            step3.CardinalityComboBox.SelectedItem = RelationshipCardinality.Multiple.ToString();
        //--------------Assert Preconditions ---------------
//            Assert.IsTrue(step3.CreateFKCheckBox.Checked);
//            Assert.IsTrue(step3.CardinalityComboBox.Enabled);
//            Assert.AreEqual("One-To-Many", step3.CardinalityComboBox.SelectedItem);
        //---------------Execute Test ----------------------
//            string message;
//            bool canMoveOn = step3.CanMoveOn(out message);
        //---------------Test Result -----------------------
//            Assert.IsTrue(canMoveOn);
//            Assert.AreEqual(string.Empty, message);
//            Assert.IsTrue(relationshipCreator.MustCreateForeignKeyProp);
//            Assert.AreEqual(RelationshipCardinality.Multiple, relationshipCreator.RelationshipCardinality);
//        }
//
//        [Test]
//        public void Test_InitialiseStep_ShouldCreateComboBox_WithCorrectOptions()
//        {
        //---------------Set up test pack-------------------
//            DMRelationshipCreator relationshipCreator;
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3)Getstep3(out relationshipCreator);
        //---------------Assert Precondition----------------
//
        //---------------Execute Test ----------------------
//            step3.InitialiseStep();
        //---------------Test Result -----------------------
//            Assert.IsNotNull(step3.CardinalityComboBox);
//            Assert.IsInstanceOf(typeof (IComboBox), step3.CardinalityComboBox);
//            Assert.AreEqual(3,step3.CardinalityComboBox.Items.Count,"This includes one blank item"); 
//        }
//
//        [Test]
//        public void Test_CanMoveOn_WhenSelectedCreateFK_AndCardinalitySetSingle_ShouldBeTrue()
//        {
        //---------------Set up test pack-------------------
//            DMRelationshipCreator relationshipCreator;
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3)Getstep3(out relationshipCreator);
//            step3.InitialiseStep();
//            step3.CreateFKCheckBox.Checked = true;
//            step3.CardinalityComboBox.SelectedItem = "Many-To-One";
        //--------------Assert Preconditions ---------------
//            Assert.IsTrue(step3.CreateFKCheckBox.Checked);
//            Assert.IsTrue(step3.CardinalityComboBox.Enabled);
//            Assert.AreEqual("Many-To-One", step3.CardinalityComboBox.SelectedItem);
        //---------------Execute Test ----------------------
//            string message;
//            bool canMoveOn = step3.CanMoveOn(out message);
        //---------------Test Result -----------------------
//            Assert.IsTrue(canMoveOn);
//            Assert.AreEqual(string.Empty, message);
//            Assert.IsTrue(relationshipCreator.MustCreateForeignKeyProp);
//            Assert.AreEqual(RelationshipCardinality.Single, relationshipCreator.RelationshipCardinality);
//        }
//
//        [Test]
//        public void Test_CanMoveOn_WhenSelectedCreateFK_AndCardinalityNotSet_ShouldBeFalse()
//        {
        //---------------Set up test pack-------------------
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3)Getstep3();
//            step3.InitialiseStep();
//            step3.CreateFKCheckBox.Checked = true;
//            step3.CardinalityComboBox.SelectedItem = null;
        //--------------Assert Preconditions ---------------
//            Assert.IsTrue(step3.CreateFKCheckBox.Checked);
//            Assert.IsTrue(step3.CardinalityComboBox.Enabled);
//            Assert.IsNull(step3.CardinalityComboBox.SelectedItem);
        //---------------Execute Test ----------------------
//            string message;
//            bool canMoveOn = step3.CanMoveOn(out message);
        //---------------Test Result -----------------------
//            Assert.IsFalse(canMoveOn);
//            Assert.AreEqual("You cannot move on since you have selected to create a ForeignKey but no cardinality has been selected", message);
//        }
//
//        [Test]
//        public void Test_CanMoveOn_WhenNotSelectedCreateFK_AndCardinalityNotSet_ShouldBeTrue()
//        {
        //---------------Set up test pack-------------------
//            DMRelationshipCreator relationshipCreator;
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3)Getstep3(out relationshipCreator);
//            step3.InitialiseStep();
//            step3.CreateFKCheckBox.Checked = false;
//            step3.CardinalityComboBox.SelectedItem = null;
        //--------------Assert Preconditions ---------------
//            Assert.IsFalse(step3.CreateFKCheckBox.Checked);
//            Assert.IsNull(step3.CardinalityComboBox.SelectedItem);
        //---------------Execute Test ----------------------
//            string message;
//            bool canMoveOn = step3.CanMoveOn(out message);
        //---------------Test Result -----------------------
//            Assert.IsTrue(canMoveOn);
//            Assert.AreEqual(string.Empty, message);
//            Assert.IsFalse(relationshipCreator.MustCreateForeignKeyProp);
//            Assert.AreEqual(RelationshipCardinality.Single, relationshipCreator.RelationshipCardinality);
//        }
//
//        [Test]
//        public void Test_CanMoveOn_WhenSelectedCreateFK_AndCardinalitySetMultiple_ShouldBeSetupRelationship()
//        {
        //---------------Set up test pack-------------------
//            DMRelationshipCreator relationshipCreator;
//            CreateRelationshipWizardStep3 step3 = (CreateRelationshipWizardStep3)Getstep3(out relationshipCreator);
//            step3.InitialiseStep();
//            step3.CreateFKCheckBox.Checked = true;
//            step3.CardinalityComboBox.SelectedItem = RelationshipCardinality.Multiple.ToString();
        //--------------Assert Preconditions ---------------
//            Assert.IsTrue(step3.CreateFKCheckBox.Checked);
//            Assert.IsTrue(step3.CardinalityComboBox.Enabled);
//            Assert.AreEqual("One-To-Many", step3.CardinalityComboBox.SelectedItem);
        //---------------Execute Test ----------------------
//            string message;
//            bool canMoveOn = step3.CanMoveOn(out message);
        //---------------Test Result -----------------------
//            Assert.IsTrue(canMoveOn,message);
//            Assert.AreEqual(string.Empty, message);
//            Assert.IsTrue(relationshipCreator.MustCreateForeignKeyProp);
//            Assert.AreEqual(RelationshipCardinality.Multiple, relationshipCreator.RelationshipCardinality);
//            DMRelationship relationship = relationshipCreator.Relationship;
//            Assert.IsNotNull(relationship.RelatedClass);
//            Assert.AreEqual(StringUtilities.Pluralize(relationship.RelatedClass.ClassNameBO), relationship.RelationshipName);
//            Assert.AreEqual(1, relationship.RelationshipProperties.Count);
//        }
        private static IWizardStep Getstep3(out DMRelationshipCreator relationshipCreator)
        {
            DMClass ownerClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            TestUtilsShared.CreateSavedObjectIdentityInfo(ownerClass);
            relationshipCreator = new DMRelationshipCreator(ownerClass)
                                      {
                                          RelatedDMClass = TestUtilsDMClass.CreateUnsavedValidDMClass()
                                      };
            return new CreateRelationshipWizardStep3(relationshipCreator, GlobalUIRegistry.ControlFactory);
        }

        private static IWizardStep Getstep3()
        {
            DMClass ownerClass = TestUtilsDMClass.CreateUnsavedValidDMClass();
            DMRelationshipCreator relationshipCreator = new DMRelationshipCreator(ownerClass)
                                                            {
                                                                RelatedDMClass =
                                                                    TestUtilsDMClass.CreateUnsavedValidDMClass()
                                                            };
            return new CreateRelationshipWizardStep3(relationshipCreator, GlobalUIRegistry.ControlFactory);
        }
    }
}